package com.study.example.gameserver.module.backpack.controller;

import com.google.common.base.Preconditions;
import com.sparrow.event.annotation.EventReceiver;
import com.sparrow.example_message.backpack.req.ReqBackPackOrganize;
import com.sparrow.example_message.backpack.req.ReqBackPackUseGoods;
import com.sparrow.example_message.backpack.resp.RespBackPackOrganize;
import com.sparrow.example_message.backpack.resp.RespBackPackUseGoods;
import com.sparrow.resource.cache.ResourceCache;
import com.sparrow.socket.tcp.anno.PacketHandler;
import com.study.example.gameserver.base.def.CDType;
import com.study.example.gameserver.module.backpack.entity.Item;
import com.study.example.gameserver.module.backpack.entity.PlayerBackPack;
import com.study.example.gameserver.module.backpack.resource.ItemResource;
import com.study.example.gameserver.module.backpack.service.BackPackService;
import com.study.example.gameserver.module.player.domain.PlayerDataType;
import com.study.example.gameserver.module.player.entity.Player;
import com.study.example.gameserver.module.player.event.PlayerInitEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@PacketHandler
@Component
public class BackPackController {
    private static Logger logger = LoggerFactory.getLogger(BackPackController.class);
    @Autowired
    private BackPackService backPackService;
    @Autowired
    private ResourceCache<Integer, ItemResource> itemResCache;
    private static final int SORT_CD = 10000;

    @EventReceiver(order = 2)
    public void init(PlayerInitEvent event) {
        Player player = event.getPlayer();
        logger.info("player{} 背包初始化通知", player.getPlayerId());
        backPackService.organize(player);

        backPackService.sendBackPackInfo(player);
    }

    @EventReceiver
    public void reqOrganize(Player player, ReqBackPackOrganize req) {
        if (player.getCdBox().isCoding(CDType.BackPackSortCD, SORT_CD)) {
            player.sendTip("整理cd");
            return;
        }
        backPackService.organize(player);
        backPackService.sendBackPackInfo(player);
        player.sendPacket(new RespBackPackOrganize());
    }

    @EventReceiver
    public void reqUseGoods(Player player, ReqBackPackUseGoods req) {
        PlayerBackPack playerBackPack = player.getData(PlayerDataType.BackPack);
        Item item = playerBackPack.getItems().get(req.getGoodsId());
        if (item == null) {
            player.sendTip("物品不存在");
            return;
        }
        if (req.getNum() <= 0 || req.getNum() > item.getNum()) {
            player.sendTip("非法操作");
            return;
        }
        ItemResource itemRes = itemResCache.getResource(item.getModelId());
        Preconditions.checkNotNull(itemRes);
        if (!backPackService.useGoods(player, itemRes, req.getNum())) {
            return;
        }
        backPackService.consumeGoods(player, req.getGoodsId(), req.getNum());

        RespBackPackUseGoods packet = new RespBackPackUseGoods();
        packet.setGoodsId(req.getGoodsId());
        packet.setNum(req.getNum());
        player.sendPacket(packet);
    }

}
